<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>geosnap._community &#8212; geosnap v0.3.1 Manual</title>
    <link rel="stylesheet" href="../../_static/bootstrap-sphinx.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../_static/pysal-styles.css" />
    <script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../_static/language_data.js"></script>
    <link rel="shortcut icon" href="../../_static/geosnap.ico"/>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1'>
<meta name="apple-mobile-web-app-capable" content="yes">
<script type="text/javascript" src="../../_static/js/jquery-1.11.0.min.js "></script>
<script type="text/javascript" src="../../_static/js/jquery-fix.js "></script>
<script type="text/javascript" src="../../_static/bootstrap-3.3.7/js/bootstrap.min.js "></script>
<script type="text/javascript" src="../../_static/bootstrap-sphinx.js "></script>

  </head><body>

  <div id="navbar" class="navbar navbar-default navbar-fixed-top">
    <div class="container">
      <div class="navbar-header">
        <!-- .btn-navbar is used as the toggle for collapsed navbar content -->
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="../../index.html"><span><img src="../../_static/geosnap.svg"></span>
          geosnap</a>
        <span class="navbar-text navbar-version pull-left"><b>0.3.1</b></span>
      </div>

        <div class="collapse navbar-collapse nav-collapse">
          <ul class="nav navbar-nav">
            
                <li><a href="../../installation.html">Installation</a></li>
                <li><a href="../../api.html">API</a></li>
                <li><a href="../../data.html">Data</a></li>
                <li><a href="https://spatialucr.github.io/geosnap-guide/">User Guide</a></li>
                <li><a href="../../references.html">References</a></li>
            
            
              <li class="dropdown globaltoc-container">
  <a role="button"
     id="dLabelGlobalToc"
     data-toggle="dropdown"
     data-target="#"
     href="../../index.html">Site <b class="caret"></b></a>
  <ul class="dropdown-menu globaltoc"
      role="menu"
      aria-labelledby="dLabelGlobalToc"><p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../installation.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../installation.html#installing-a-released-version">Installing a released version</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../installation.html#installing-development-version">Installing development version</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../api.html">API</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../api.html#io-module">IO Module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../api.html#the-community-class">The Community Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../api.html#analyze-module">Analyze Module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../api.html#harmonize-module">Harmonize Module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../api.html#visualize-module">Visualize Module</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../references.html">References</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../data.html">Data</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../data.html#tabular-data">Tabular Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../data.html#geo-data">Geo Data</a></li>
</ul>
</li>
</ul>
</ul>
</li>
              
                <li class="dropdown">
  <a role="button"
     id="dLabelLocalToc"
     data-toggle="dropdown"
     data-target="#"
     href="#">Page <b class="caret"></b></a>
  <ul class="dropdown-menu localtoc"
      role="menu"
      aria-labelledby="dLabelLocalToc"></ul>
</li>
              
            
            
            
            
            
          </ul>

          
            
<form class="navbar-form navbar-right" action="../../search.html" method="get">
 <div class="form-group">
  <input type="text" name="q" class="form-control" placeholder="Search" />
 </div>
  <input type="hidden" name="check_keywords" value="yes" />
  <input type="hidden" name="area" value="default" />
</form>
          
        </div>
    </div>
  </div>

<div class="container">
  <div class="row">
    <div class="body col-md-12 content" role="main">
      
  <h1>Source code for geosnap._community</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;A Community is a thin wrapper around a long-form time-series geodataframe.&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">warnings</span> <span class="kn">import</span> <span class="n">warn</span>

<span class="kn">import</span> <span class="nn">geopandas</span> <span class="k">as</span> <span class="nn">gpd</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>

<span class="kn">from</span> <span class="nn">._data</span> <span class="kn">import</span> <span class="n">_Map</span><span class="p">,</span> <span class="n">datasets</span>
<span class="kn">from</span> <span class="nn">.analyze</span> <span class="kn">import</span> <span class="n">cluster</span> <span class="k">as</span> <span class="n">_cluster</span>
<span class="kn">from</span> <span class="nn">.analyze</span> <span class="kn">import</span> <span class="n">cluster_spatial</span> <span class="k">as</span> <span class="n">_cluster_spatial</span>
<span class="kn">from</span> <span class="nn">.analyze</span> <span class="kn">import</span> <span class="n">sequence</span> <span class="k">as</span> <span class="n">_sequence</span>
<span class="kn">from</span> <span class="nn">.analyze</span> <span class="kn">import</span> <span class="n">transition</span> <span class="k">as</span> <span class="n">_transition</span>
<span class="kn">from</span> <span class="nn">.harmonize</span> <span class="kn">import</span> <span class="n">harmonize</span> <span class="k">as</span> <span class="n">_harmonize</span>
<span class="kn">from</span> <span class="nn">.io</span> <span class="kn">import</span> <span class="n">_fips_filter</span><span class="p">,</span> <span class="n">_fipstable</span><span class="p">,</span> <span class="n">_from_db</span><span class="p">,</span> <span class="n">get_lehd</span>


<div class="viewcode-block" id="Community"><a class="viewcode-back" href="../../generated/geosnap.Community.html#geosnap.Community">[docs]</a><span class="k">class</span> <span class="nc">Community</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Spatial and tabular data for a collection of &quot;neighborhoods&quot; over time.</span>

<span class="sd">       A community is a collection of &quot;neighborhoods&quot; represented by spatial</span>
<span class="sd">       boundaries (e.g. census tracts, or blocks in the US), and tabular data</span>
<span class="sd">       which describe the composition of each neighborhood (e.g. data from</span>
<span class="sd">       surveys, sensors, or geocoded misc.). A Community can be large (e.g. a</span>
<span class="sd">       metropolitan region), or small (e.g. a handfull of census tracts) and</span>
<span class="sd">       may have data pertaining to multiple discrete points in time.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    gdf : geopandas.GeoDataFrame</span>
<span class="sd">        long-form geodataframe that holds spatial and tabular data.</span>
<span class="sd">    harmonized : bool</span>
<span class="sd">        Whether neighborhood boundaries have been harmonized into a set of</span>
<span class="sd">        time-consistent units</span>
<span class="sd">    **kwargs</span>


<span class="sd">    Attributes</span>
<span class="sd">    ----------</span>
<span class="sd">    gdf : geopandas.GeoDataFrame</span>
<span class="sd">        long-form geodataframe that stores neighborhood-level attributes</span>
<span class="sd">        and geometries for one or more time periods</span>
<span class="sd">    harmonized : bool</span>
<span class="sd">        Whether neighborhood boundaries have been harmonized into</span>
<span class="sd">        consistent units over time</span>
<span class="sd">    models : dict</span>
<span class="sd">        Dictionary of model instances that have been fitted on the community.</span>
<span class="sd">        The model name is the key and the value is a named tuple that stores the input matrix,</span>
<span class="sd">        the columns used to fit the model, the cluster labels, and the spatial weights matrix</span>
<span class="sd">        if necessary. For cluster models, the model name will match a column on the Community.gdf.</span>

<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="Community.__init__"><a class="viewcode-back" href="../../generated/geosnap.Community.html#geosnap.Community.__init__">[docs]</a>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gdf</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">harmonized</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize a new Community.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        gdf : geopandas.GeoDataFrame</span>
<span class="sd">            long-form geodataframe that stores neighborhood-level attributes</span>
<span class="sd">            and geometries for one or more time periods</span>
<span class="sd">        harmonized : bool</span>
<span class="sd">            Whether neighborhood boundaries have been harmonized into</span>
<span class="sd">            consistent units over time</span>
<span class="sd">        **kwargs : kwargs</span>
<span class="sd">            extra keyword arguments `**kwargs`.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gdf</span> <span class="o">=</span> <span class="n">gdf</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">harmonized</span> <span class="o">=</span> <span class="n">harmonized</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">models</span> <span class="o">=</span> <span class="n">_Map</span><span class="p">()</span></div>

<div class="viewcode-block" id="Community.harmonize"><a class="viewcode-back" href="../../generated/geosnap.Community.harmonize.html#geosnap.Community.harmonize">[docs]</a>    <span class="k">def</span> <span class="nf">harmonize</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">target_year</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">weights_method</span><span class="o">=</span><span class="s2">&quot;area&quot;</span><span class="p">,</span>
        <span class="n">extensive_variables</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">intensive_variables</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">allocate_total</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
        <span class="n">raster</span><span class="o">=</span><span class="s2">&quot;nlcd_2011&quot;</span><span class="p">,</span>
        <span class="n">codes</span><span class="o">=</span><span class="s2">&quot;developed&quot;</span><span class="p">,</span>
        <span class="n">force_crs_match</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Standardize inconsistent boundaries into time-static ones.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        target_year: int</span>
<span class="sd">            Polygons from this year will become the target boundaries for</span>
<span class="sd">            spatial interpolation.</span>
<span class="sd">        weights_method : string</span>
<span class="sd">            The method that the harmonization will be conducted. This can be</span>
<span class="sd">            set to:</span>
<span class="sd">                * &quot;area&quot;                          : harmonization according to area weights.</span>
<span class="sd">                * &quot;land_type_area&quot;                : harmonization according to the Land Types considered &#39;populated&#39; areas.</span>
<span class="sd">                * &quot;land_type_Poisson_regression&quot;  : NOT YET INTRODUCED.</span>
<span class="sd">                * &quot;land_type_Gaussian_regression&quot; : NOT YET INTRODUCED.</span>
<span class="sd">        extensive_variables : list</span>
<span class="sd">            extensive variables to be used in interpolation.</span>
<span class="sd">        intensive_variables : type</span>
<span class="sd">            intensive variables to be used in interpolation.</span>
<span class="sd">        allocate_total : boolean</span>
<span class="sd">            True if total value of source area should be allocated.</span>
<span class="sd">            False if denominator is area of i. Note that the two cases</span>
<span class="sd">            would be identical when the area of the source polygon is</span>
<span class="sd">            exhausted by intersections. See (3) in Notes for more details</span>
<span class="sd">        raster_path : str</span>
<span class="sd">            path to the raster image that has the types of each pixel in the</span>
<span class="sd">            spatial context. Only taken into consideration for harmonization</span>
<span class="sd">            raster based.</span>
<span class="sd">        codes : list of ints</span>
<span class="sd">            pixel values that should be included in the regression (the default &quot;developed&quot; includes [21, 22, 23, 24]).</span>
<span class="sd">        force_crs_match : bool</span>
<span class="sd">            whether source and target dataframes should be reprojected to match (the default is True).</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        None</span>
<span class="sd">            New data are added to the input Community</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># convert the long-form into a list of dataframes</span>
        <span class="c1"># data = [x[1] for x in self.gdf.groupby(&quot;year&quot;)]</span>
        <span class="k">if</span> <span class="n">codes</span> <span class="o">==</span> <span class="s2">&quot;developed&quot;</span><span class="p">:</span>
            <span class="n">codes</span> <span class="o">=</span> <span class="p">[</span><span class="mi">21</span><span class="p">,</span> <span class="mi">22</span><span class="p">,</span> <span class="mi">23</span><span class="p">,</span> <span class="mi">24</span><span class="p">]</span>
        <span class="n">gdf</span> <span class="o">=</span> <span class="n">_harmonize</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">gdf</span><span class="p">,</span>
            <span class="n">target_year</span><span class="o">=</span><span class="n">target_year</span><span class="p">,</span>
            <span class="n">weights_method</span><span class="o">=</span><span class="n">weights_method</span><span class="p">,</span>
            <span class="n">extensive_variables</span><span class="o">=</span><span class="n">extensive_variables</span><span class="p">,</span>
            <span class="n">intensive_variables</span><span class="o">=</span><span class="n">intensive_variables</span><span class="p">,</span>
            <span class="n">allocate_total</span><span class="o">=</span><span class="n">allocate_total</span><span class="p">,</span>
            <span class="n">raster</span><span class="o">=</span><span class="n">raster</span><span class="p">,</span>
            <span class="n">codes</span><span class="o">=</span><span class="n">codes</span><span class="p">,</span>
            <span class="n">force_crs_match</span><span class="o">=</span><span class="n">force_crs_match</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">Community</span><span class="p">(</span><span class="n">gdf</span><span class="p">,</span> <span class="n">harmonized</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>

<div class="viewcode-block" id="Community.cluster"><a class="viewcode-back" href="../../generated/geosnap.Community.cluster.html#geosnap.Community.cluster">[docs]</a>    <span class="k">def</span> <span class="nf">cluster</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">n_clusters</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span>
        <span class="n">method</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">best_model</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
        <span class="n">columns</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">verbose</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
        <span class="n">scaler</span><span class="o">=</span><span class="s2">&quot;std&quot;</span><span class="p">,</span>
        <span class="n">pooling</span><span class="o">=</span><span class="s2">&quot;fixed&quot;</span><span class="p">,</span>
        <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a geodemographic typology by running a cluster analysis on the study area&#39;s neighborhood attributes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        n_clusters : int, required</span>
<span class="sd">            the number of clusters to model. The default is 6).</span>
<span class="sd">        method : str in [&#39;kmeans&#39;, &#39;ward&#39;, &#39;affinity_propagation&#39;, &#39;spectral&#39;, &#39;gaussian_mixture&#39;, &#39;hdbscan&#39;], required</span>
<span class="sd">            the clustering algorithm used to identify neighborhood types</span>
<span class="sd">        best_model : bool, optional</span>
<span class="sd">            if using a gaussian mixture model, use BIC to choose the best</span>
<span class="sd">            n_clusters. (the default is False).</span>
<span class="sd">        columns : array-like, required</span>
<span class="sd">            subset of columns on which to apply the clustering</span>
<span class="sd">        verbose : bool, optional</span>
<span class="sd">            whether to print warning messages (the default is False).</span>
<span class="sd">        scaler : None or scaler from sklearn.preprocessing, optional</span>
<span class="sd">            a scikit-learn preprocessing class that will be used to rescale the</span>
<span class="sd">            data. Defaults to sklearn.preprocessing.StandardScaler</span>
<span class="sd">        pooling : [&quot;fixed&quot;, &quot;pooled&quot;, &quot;unique&quot;], optional (default=&#39;fixed&#39;)</span>
<span class="sd">            How to treat temporal data when applying scaling. Options include:</span>

<span class="sd">            * fixed : scaling is fixed to each time period</span>
<span class="sd">            * pooled : data are pooled across all time periods</span>
<span class="sd">            * unique : if scaling, apply the scaler to each time period, then generate clusters unique to each time period.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        geosnap.Community</span>
<span class="sd">            a copy of input Community with neighborhood cluster labels appended</span>
<span class="sd">            as a new column. If the cluster is already present, the name will be incremented</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">harmonized</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">harmonized</span>
        <span class="n">gdf</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">model_name</span> <span class="o">=</span> <span class="n">_cluster</span><span class="p">(</span>
            <span class="n">gdf</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">gdf</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span>
            <span class="n">n_clusters</span><span class="o">=</span><span class="n">n_clusters</span><span class="p">,</span>
            <span class="n">method</span><span class="o">=</span><span class="n">method</span><span class="p">,</span>
            <span class="n">best_model</span><span class="o">=</span><span class="n">best_model</span><span class="p">,</span>
            <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
            <span class="n">verbose</span><span class="o">=</span><span class="n">verbose</span><span class="p">,</span>
            <span class="n">scaler</span><span class="o">=</span><span class="n">scaler</span><span class="p">,</span>
            <span class="n">pooling</span><span class="o">=</span><span class="n">pooling</span><span class="p">,</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="n">comm</span> <span class="o">=</span> <span class="n">Community</span><span class="p">(</span><span class="n">gdf</span><span class="p">,</span> <span class="n">harmonized</span><span class="o">=</span><span class="n">harmonized</span><span class="p">)</span>
        <span class="n">comm</span><span class="o">.</span><span class="n">models</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">models</span>
        <span class="p">)</span>  <span class="c1"># keep any existing models in the input Community</span>
        <span class="n">comm</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">model_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</span>
        <span class="k">return</span> <span class="n">comm</span></div>

<div class="viewcode-block" id="Community.cluster_spatial"><a class="viewcode-back" href="../../generated/geosnap.Community.cluster_spatial.html#geosnap.Community.cluster_spatial">[docs]</a>    <span class="k">def</span> <span class="nf">cluster_spatial</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">n_clusters</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span>
        <span class="n">spatial_weights</span><span class="o">=</span><span class="s2">&quot;rook&quot;</span><span class="p">,</span>
        <span class="n">method</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">best_model</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
        <span class="n">columns</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">threshold_variable</span><span class="o">=</span><span class="s2">&quot;count&quot;</span><span class="p">,</span>
        <span class="n">threshold</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
        <span class="n">return_model</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
        <span class="n">scaler</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">weights_kwargs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a *spatial* geodemographic typology by running a cluster analysis on the metro area&#39;s neighborhood attributes and including a contiguity constraint.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        n_clusters : int, required</span>
<span class="sd">            the number of clusters to model. The default is 6).</span>
<span class="sd">        spatial_weights : str (&#39;queen&#39; or &#39;rook&#39;) or libpysal.weights.W instance, optional</span>
<span class="sd">            spatial weights matrix specification` (the default is &quot;rook&quot;). If &#39;rook&#39; or &#39;queen&#39;</span>
<span class="sd">            then contiguity weights will be constructed internally, otherwise pass a</span>
<span class="sd">            libpysal.weights.W with additional arguments specified in weights_kwargs</span>
<span class="sd">        weights_kwargs : dict, optional</span>
<span class="sd">            If passing a libpysal.weights.W instance to spatial_weights, these additional</span>
<span class="sd">            keyword arguments that will be passed to the weights constructor</span>
<span class="sd">        method : str in [&#39;ward_spatial&#39;, &#39;spenc&#39;, &#39;skater&#39;, &#39;azp&#39;, &#39;max_p&#39;], required</span>
<span class="sd">            the clustering algorithm used to identify neighborhood types</span>
<span class="sd">        columns : array-like, required</span>
<span class="sd">            subset of columns on which to apply the clustering</span>
<span class="sd">        threshold_variable : str, required if using max-p, optional otherwise</span>
<span class="sd">            for max-p, which variable should define `p`. The default is &quot;count&quot;,</span>
<span class="sd">            which will grow regions until the threshold number of polygons have</span>
<span class="sd">            been aggregated</span>
<span class="sd">        threshold : numeric, optional</span>
<span class="sd">            threshold to use for max-p clustering (the default is 10).</span>
<span class="sd">        scaler : None or scaler from sklearn.preprocessing, optional</span>
<span class="sd">            a scikit-learn preprocessing class that will be used to rescale the</span>
<span class="sd">            data. Defaults to sklearn.preprocessing.StandardScaler</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        geosnap.Community</span>
<span class="sd">            a copy of input Community with neighborhood cluster labels appended</span>
<span class="sd">            as a new column. If the cluster is already present, the name will be incremented</span>


<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">harmonized</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">harmonized</span>

        <span class="n">gdf</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">model_name</span> <span class="o">=</span> <span class="n">_cluster_spatial</span><span class="p">(</span>
            <span class="n">gdf</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">gdf</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span>
            <span class="n">n_clusters</span><span class="o">=</span><span class="n">n_clusters</span><span class="p">,</span>
            <span class="n">spatial_weights</span><span class="o">=</span><span class="n">spatial_weights</span><span class="p">,</span>
            <span class="n">method</span><span class="o">=</span><span class="n">method</span><span class="p">,</span>
            <span class="n">best_model</span><span class="o">=</span><span class="n">best_model</span><span class="p">,</span>
            <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
            <span class="n">threshold_variable</span><span class="o">=</span><span class="n">threshold_variable</span><span class="p">,</span>
            <span class="n">threshold</span><span class="o">=</span><span class="n">threshold</span><span class="p">,</span>
            <span class="n">return_model</span><span class="o">=</span><span class="n">return_model</span><span class="p">,</span>
            <span class="n">scaler</span><span class="o">=</span><span class="n">scaler</span><span class="p">,</span>
            <span class="n">weights_kwargs</span><span class="o">=</span><span class="n">weights_kwargs</span><span class="p">,</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="n">comm</span> <span class="o">=</span> <span class="n">Community</span><span class="p">(</span><span class="n">gdf</span><span class="p">,</span> <span class="n">harmonized</span><span class="o">=</span><span class="n">harmonized</span><span class="p">)</span>
        <span class="n">comm</span><span class="o">.</span><span class="n">models</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">models</span>
        <span class="p">)</span>  <span class="c1"># keep any existing models in the input Community</span>
        <span class="n">comm</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">model_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</span>
        <span class="k">return</span> <span class="n">comm</span></div>

<div class="viewcode-block" id="Community.transition"><a class="viewcode-back" href="../../generated/geosnap.Community.transition.html#geosnap.Community.transition">[docs]</a>    <span class="k">def</span> <span class="nf">transition</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">cluster_col</span><span class="p">,</span> <span class="n">time_var</span><span class="o">=</span><span class="s2">&quot;year&quot;</span><span class="p">,</span> <span class="n">id_var</span><span class="o">=</span><span class="s2">&quot;geoid&quot;</span><span class="p">,</span> <span class="n">w_type</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">permutations</span><span class="o">=</span><span class="mi">0</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        (Spatial) Markov approach to transitional dynamics of neighborhoods.</span>

<span class="sd">        The transitional dynamics approach should be adopted after</span>
<span class="sd">        neighborhood segmentation since the column name of neighborhood</span>
<span class="sd">        labels is a required input.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        cluster_col     : string or int</span>
<span class="sd">                          Column name for the neighborhood segmentation, such as</span>
<span class="sd">                          &quot;ward&quot;, &quot;kmeans&quot;, etc.</span>
<span class="sd">        time_var        : string, optional</span>
<span class="sd">                          Column defining time and or sequencing of the long-form data.</span>
<span class="sd">                          Default is &quot;year&quot;.</span>
<span class="sd">        id_var          : string, optional</span>
<span class="sd">                          Column identifying the unique id of spatial units.</span>
<span class="sd">                          Default is &quot;geoid&quot;.</span>
<span class="sd">        w_type          : string, optional</span>
<span class="sd">                          Type of spatial weights type (&quot;rook&quot;, &quot;queen&quot;, &quot;knn&quot; or</span>
<span class="sd">                          &quot;kernel&quot;) to be used for spatial structure. Default is</span>
<span class="sd">                          None, if non-spatial Markov transition rates are desired.</span>
<span class="sd">        permutations    : int, optional</span>
<span class="sd">                          number of permutations for use in randomization based</span>
<span class="sd">                          inference (the default is 0).</span>


<span class="sd">        Returns</span>
<span class="sd">        ---------</span>
<span class="sd">        mar             : giddy.markov.Markov or giddy.markov.Spatial_Markov</span>
<span class="sd">                          if w_type=None, return a classic Markov instance;</span>
<span class="sd">                          if w_type is given, return a Spatial_Markov instance</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">mar</span> <span class="o">=</span> <span class="n">_transition</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">gdf</span><span class="p">,</span>
            <span class="n">cluster_col</span><span class="p">,</span>
            <span class="n">time_var</span><span class="o">=</span><span class="n">time_var</span><span class="p">,</span>
            <span class="n">id_var</span><span class="o">=</span><span class="n">id_var</span><span class="p">,</span>
            <span class="n">w_type</span><span class="o">=</span><span class="n">w_type</span><span class="p">,</span>
            <span class="n">permutations</span><span class="o">=</span><span class="n">permutations</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">mar</span></div>

<div class="viewcode-block" id="Community.sequence"><a class="viewcode-back" href="../../generated/geosnap.Community.sequence.html#geosnap.Community.sequence">[docs]</a>    <span class="k">def</span> <span class="nf">sequence</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">cluster_col</span><span class="p">,</span>
        <span class="n">seq_clusters</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>
        <span class="n">subs_mat</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">dist_type</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">indel</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">time_var</span><span class="o">=</span><span class="s2">&quot;year&quot;</span><span class="p">,</span>
        <span class="n">id_var</span><span class="o">=</span><span class="s2">&quot;geoid&quot;</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Pairwise sequence analysis to evaluate the distance/dissimilarity</span>
<span class="sd">        between every two neighborhood sequences.</span>

<span class="sd">        The sequence approach should be adopted after</span>
<span class="sd">        neighborhood segmentation since the column name of neighborhood</span>
<span class="sd">        labels is a required input.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        cluster_col     : string or int</span>
<span class="sd">                          Column name for the neighborhood segmentation, such as</span>
<span class="sd">                          &quot;ward&quot;, &quot;kmeans&quot;, etc.</span>
<span class="sd">        seq_clusters    : int, optional</span>
<span class="sd">                          Number of neighborhood sequence clusters. Agglomerative</span>
<span class="sd">                          Clustering with Ward linkage is now used for clustering</span>
<span class="sd">                          the sequences. Default is 5.</span>
<span class="sd">        subs_mat        : array</span>
<span class="sd">                          (k,k), substitution cost matrix. Should be hollow (</span>
<span class="sd">                          0 cost between the same type), symmetric and non-negative.</span>
<span class="sd">        dist_type       : string</span>
<span class="sd">                          &quot;hamming&quot;: hamming distance (substitution only</span>
<span class="sd">                          and its cost is constant 1) from sklearn.metrics;</span>
<span class="sd">                          &quot;markov&quot;: utilize empirical transition</span>
<span class="sd">                          probabilities to define substitution costs;</span>
<span class="sd">                          &quot;interval&quot;: differences between states are used</span>
<span class="sd">                          to define substitution costs, and indel=k-1;</span>
<span class="sd">                          &quot;arbitrary&quot;: arbitrary distance if there is not a</span>
<span class="sd">                          strong theory guidance: substitution=0.5, indel=1.</span>
<span class="sd">                          &quot;tran&quot;: transition-oriented optimal matching. Sequence of</span>
<span class="sd">                          transitions. Based on :cite:`Biemann:2011`.</span>
<span class="sd">        indel           : float, optional</span>
<span class="sd">                          insertion/deletion cost.</span>
<span class="sd">        time_var        : string, optional</span>
<span class="sd">                          Column defining time and or sequencing of the long-form data.</span>
<span class="sd">                          Default is &quot;year&quot;.</span>
<span class="sd">        id_var          : string, optional</span>
<span class="sd">                          Column identifying the unique id of spatial units.</span>
<span class="sd">                          Default is &quot;geoid&quot;.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        gdf_new         : Community instance</span>
<span class="sd">                          New Community instance with attribute &quot;gdf&quot; having</span>
<span class="sd">                          a new column for sequence labels.</span>
<span class="sd">        df_wide         : pandas.DataFrame</span>
<span class="sd">                          Wide-form DataFrame with k (k is the number of periods)</span>
<span class="sd">                          columns of neighborhood types and 1 column of sequence</span>
<span class="sd">                          labels.</span>
<span class="sd">        seq_dis_mat     : array</span>
<span class="sd">                          (n,n), distance/dissimilarity matrix for each pair of</span>
<span class="sd">                          sequences</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">gdf_temp</span><span class="p">,</span> <span class="n">df_wide</span><span class="p">,</span> <span class="n">seq_dis_mat</span> <span class="o">=</span> <span class="n">_sequence</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">gdf</span><span class="p">,</span>
            <span class="n">cluster_col</span><span class="p">,</span>
            <span class="n">seq_clusters</span><span class="o">=</span><span class="n">seq_clusters</span><span class="p">,</span>
            <span class="n">subs_mat</span><span class="o">=</span><span class="n">subs_mat</span><span class="p">,</span>
            <span class="n">dist_type</span><span class="o">=</span><span class="n">dist_type</span><span class="p">,</span>
            <span class="n">indel</span><span class="o">=</span><span class="n">indel</span><span class="p">,</span>
            <span class="n">time_var</span><span class="o">=</span><span class="n">time_var</span><span class="p">,</span>
            <span class="n">id_var</span><span class="o">=</span><span class="n">id_var</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">gdf_new</span> <span class="o">=</span> <span class="n">Community</span><span class="p">(</span><span class="n">gdf_temp</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">gdf_new</span><span class="p">,</span> <span class="n">df_wide</span><span class="p">,</span> <span class="n">seq_dis_mat</span></div>

<div class="viewcode-block" id="Community.from_ltdb"><a class="viewcode-back" href="../../generated/geosnap.Community.from_ltdb.html#geosnap.Community.from_ltdb">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_ltdb</span><span class="p">(</span>
        <span class="bp">cls</span><span class="p">,</span>
        <span class="n">state_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">county_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">msa_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">boundary</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">years</span><span class="o">=</span><span class="s2">&quot;all&quot;</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new Community from LTDB data.</span>

<span class="sd">           Instiantiate a new Community from pre-harmonized LTDB data. To use</span>
<span class="sd">           you must first download and register LTDB data with geosnap using</span>
<span class="sd">           the `store_ltdb` function. Pass lists of states, counties, or any</span>
<span class="sd">           arbitrary FIPS codes to create a community. All fips code arguments</span>
<span class="sd">           are additive, so geosnap will include the largest unique set.</span>
<span class="sd">           Alternatively, you may provide a boundary to use as a clipping</span>
<span class="sd">           feature.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        state_fips : list or str</span>
<span class="sd">            string or list of strings of two-digit fips codes defining states</span>
<span class="sd">            to include in the study area.</span>
<span class="sd">        county_fips : list or str</span>
<span class="sd">            string or list of strings of five-digit fips codes defining</span>
<span class="sd">            counties to include in the study area.</span>
<span class="sd">        msa_fips : list or str</span>
<span class="sd">            string or list of strings of fips codes defining</span>
<span class="sd">            MSAs to include in the study area.</span>
<span class="sd">        fips : list or str</span>
<span class="sd">            string or list of strings of five-digit fips codes defining</span>
<span class="sd">            counties to include in the study area.</span>
<span class="sd">        boundary : geopandas.GeoDataFrame</span>
<span class="sd">            geodataframe that defines the total extent of the study area.</span>
<span class="sd">            This will be used to clip tracts lazily by selecting all</span>
<span class="sd">            `GeoDataFrame.representative_point()`s that intersect the</span>
<span class="sd">            boundary gdf</span>
<span class="sd">        years : list of ints</span>
<span class="sd">            list of years (decades) to include in the study data</span>
<span class="sd">            (the default &quot;all&quot; is [1970, 1980, 1990, 2000, 2010]).</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Community</span>
<span class="sd">            Community with LTDB data</span>


<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">years</span> <span class="o">==</span> <span class="s2">&quot;all&quot;</span><span class="p">:</span>
            <span class="n">years</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1970</span><span class="p">,</span> <span class="mi">1980</span><span class="p">,</span> <span class="mi">1990</span><span class="p">,</span> <span class="mi">2000</span><span class="p">,</span> <span class="mi">2010</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">boundary</span><span class="p">,</span> <span class="n">gpd</span><span class="o">.</span><span class="n">GeoDataFrame</span><span class="p">):</span>
            <span class="n">tracts</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">tracts_2010</span><span class="p">()[[</span><span class="s2">&quot;geoid&quot;</span><span class="p">,</span> <span class="s2">&quot;geometry&quot;</span><span class="p">]]</span>
            <span class="n">ltdb</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">ltdb</span><span class="p">()</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">boundary</span><span class="o">.</span><span class="n">crs</span> <span class="o">!=</span> <span class="n">tracts</span><span class="o">.</span><span class="n">crs</span><span class="p">:</span>
                <span class="n">warn</span><span class="p">(</span>
                    <span class="s2">&quot;Unable to determine whether boundary CRS is WGS84 &quot;</span>
                    <span class="s2">&quot;if this produces unexpected results, try reprojecting&quot;</span>
                <span class="p">)</span>
            <span class="n">tracts</span> <span class="o">=</span> <span class="n">tracts</span><span class="p">[</span>
                <span class="n">tracts</span><span class="o">.</span><span class="n">representative_point</span><span class="p">()</span><span class="o">.</span><span class="n">intersects</span><span class="p">(</span><span class="n">boundary</span><span class="o">.</span><span class="n">unary_union</span><span class="p">)</span>
            <span class="p">]</span>
            <span class="n">gdf</span> <span class="o">=</span> <span class="n">ltdb</span><span class="p">[</span><span class="n">ltdb</span><span class="p">[</span><span class="s2">&quot;geoid&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">isin</span><span class="p">(</span><span class="n">tracts</span><span class="p">[</span><span class="s2">&quot;geoid&quot;</span><span class="p">])]</span>
            <span class="n">gdf</span> <span class="o">=</span> <span class="n">gpd</span><span class="o">.</span><span class="n">GeoDataFrame</span><span class="p">(</span><span class="n">gdf</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">tracts</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s2">&quot;geoid&quot;</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s2">&quot;left&quot;</span><span class="p">))</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">gdf</span> <span class="o">=</span> <span class="n">_from_db</span><span class="p">(</span>
                <span class="n">data</span><span class="o">=</span><span class="n">datasets</span><span class="o">.</span><span class="n">ltdb</span><span class="p">(),</span>
                <span class="n">state_fips</span><span class="o">=</span><span class="n">state_fips</span><span class="p">,</span>
                <span class="n">county_fips</span><span class="o">=</span><span class="n">county_fips</span><span class="p">,</span>
                <span class="n">msa_fips</span><span class="o">=</span><span class="n">msa_fips</span><span class="p">,</span>
                <span class="n">fips</span><span class="o">=</span><span class="n">fips</span><span class="p">,</span>
                <span class="n">years</span><span class="o">=</span><span class="n">years</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">gdf</span><span class="o">=</span><span class="n">gdf</span><span class="o">.</span><span class="n">reset_index</span><span class="p">(),</span> <span class="n">harmonized</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>

<div class="viewcode-block" id="Community.from_ncdb"><a class="viewcode-back" href="../../generated/geosnap.Community.from_ncdb.html#geosnap.Community.from_ncdb">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_ncdb</span><span class="p">(</span>
        <span class="bp">cls</span><span class="p">,</span>
        <span class="n">state_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">county_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">msa_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">boundary</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">years</span><span class="o">=</span><span class="s2">&quot;all&quot;</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new Community from NCDB data.</span>

<span class="sd">           Instiantiate a new Community from pre-harmonized NCDB data. To use</span>
<span class="sd">           you must first download and register LTDB data with geosnap using</span>
<span class="sd">           the `store_ncdb` function. Pass lists of states, counties, or any</span>
<span class="sd">           arbitrary FIPS codes to create a community. All fips code arguments</span>
<span class="sd">           are additive, so geosnap will include the largest unique set.</span>
<span class="sd">           Alternatively, you may provide a boundary to use as a clipping</span>
<span class="sd">           feature.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        state_fips : list or str</span>
<span class="sd">            string or list of strings of two-digit fips codes defining states</span>
<span class="sd">            to include in the study area.</span>
<span class="sd">        county_fips : list or str</span>
<span class="sd">            string or list of strings of five-digit fips codes defining</span>
<span class="sd">            counties to include in the study area.</span>
<span class="sd">        msa_fips : list or str</span>
<span class="sd">            string or list of strings of fips codes defining</span>
<span class="sd">            MSAs to include in the study area.</span>
<span class="sd">        fips : list or str</span>
<span class="sd">            string or list of strings of five-digit fips codes defining</span>
<span class="sd">            counties to include in the study area.</span>
<span class="sd">        boundary : geopandas.GeoDataFrame</span>
<span class="sd">            geodataframe that defines the total extent of the study area.</span>
<span class="sd">            This will be used to clip tracts lazily by selecting all</span>
<span class="sd">            `GeoDataFrame.representative_point()`s that intersect the</span>
<span class="sd">            boundary gdf</span>
<span class="sd">        years : list of ints</span>
<span class="sd">            list of years (decades) to include in the study data</span>
<span class="sd">            (the default is all available [1970, 1980, 1990, 2000, 2010]).</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Community</span>
<span class="sd">            Community with NCDB data</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">years</span> <span class="o">==</span> <span class="s2">&quot;all&quot;</span><span class="p">:</span>
            <span class="n">years</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1970</span><span class="p">,</span> <span class="mi">1980</span><span class="p">,</span> <span class="mi">1990</span><span class="p">,</span> <span class="mi">2000</span><span class="p">,</span> <span class="mi">2010</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">boundary</span><span class="p">,</span> <span class="n">gpd</span><span class="o">.</span><span class="n">GeoDataFrame</span><span class="p">):</span>
            <span class="n">tracts</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">tracts_2010</span><span class="p">()[[</span><span class="s2">&quot;geoid&quot;</span><span class="p">,</span> <span class="s2">&quot;geometry&quot;</span><span class="p">]]</span>
            <span class="n">ncdb</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">ncdb</span><span class="p">()</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">boundary</span><span class="o">.</span><span class="n">crs</span> <span class="o">!=</span> <span class="n">tracts</span><span class="o">.</span><span class="n">crs</span><span class="p">:</span>
                <span class="n">warn</span><span class="p">(</span>
                    <span class="s2">&quot;Unable to determine whether boundary CRS is WGS84 &quot;</span>
                    <span class="s2">&quot;if this produces unexpected results, try reprojecting&quot;</span>
                <span class="p">)</span>
            <span class="n">tracts</span> <span class="o">=</span> <span class="n">tracts</span><span class="p">[</span>
                <span class="n">tracts</span><span class="o">.</span><span class="n">representative_point</span><span class="p">()</span><span class="o">.</span><span class="n">intersects</span><span class="p">(</span><span class="n">boundary</span><span class="o">.</span><span class="n">unary_union</span><span class="p">)</span>
            <span class="p">]</span>
            <span class="n">gdf</span> <span class="o">=</span> <span class="n">ncdb</span><span class="p">[</span><span class="n">ncdb</span><span class="p">[</span><span class="s2">&quot;geoid&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">isin</span><span class="p">(</span><span class="n">tracts</span><span class="p">[</span><span class="s2">&quot;geoid&quot;</span><span class="p">])]</span>
            <span class="n">gdf</span> <span class="o">=</span> <span class="n">gpd</span><span class="o">.</span><span class="n">GeoDataFrame</span><span class="p">(</span><span class="n">gdf</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">tracts</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s2">&quot;geoid&quot;</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s2">&quot;left&quot;</span><span class="p">))</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">gdf</span> <span class="o">=</span> <span class="n">_from_db</span><span class="p">(</span>
                <span class="n">data</span><span class="o">=</span><span class="n">datasets</span><span class="o">.</span><span class="n">ncdb</span><span class="p">(),</span>
                <span class="n">state_fips</span><span class="o">=</span><span class="n">state_fips</span><span class="p">,</span>
                <span class="n">county_fips</span><span class="o">=</span><span class="n">county_fips</span><span class="p">,</span>
                <span class="n">msa_fips</span><span class="o">=</span><span class="n">msa_fips</span><span class="p">,</span>
                <span class="n">fips</span><span class="o">=</span><span class="n">fips</span><span class="p">,</span>
                <span class="n">years</span><span class="o">=</span><span class="n">years</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">gdf</span><span class="o">=</span><span class="n">gdf</span><span class="o">.</span><span class="n">reset_index</span><span class="p">(),</span> <span class="n">harmonized</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>

<div class="viewcode-block" id="Community.from_census"><a class="viewcode-back" href="../../generated/geosnap.Community.from_census.html#geosnap.Community.from_census">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_census</span><span class="p">(</span>
        <span class="bp">cls</span><span class="p">,</span>
        <span class="n">state_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">county_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">msa_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">boundary</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">years</span><span class="o">=</span><span class="s2">&quot;all&quot;</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new Community from original vintage US Census data.</span>

<span class="sd">           Instiantiate a new Community from . To use</span>
<span class="sd">           you must first download and register census data with geosnap using</span>
<span class="sd">           the `store_census` function. Pass lists of states, counties, or any</span>
<span class="sd">           arbitrary FIPS codes to create a community. All fips code arguments</span>
<span class="sd">           are additive, so geosnap will include the largest unique set.</span>
<span class="sd">           Alternatively, you may provide a boundary to use as a clipping</span>
<span class="sd">           feature.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        state_fips : list or str, optional</span>
<span class="sd">            string or list of strings of two-digit fips codes defining states</span>
<span class="sd">            to include in the study area.</span>
<span class="sd">        county_fips : list or str, optional</span>
<span class="sd">            string or list of strings of five-digit fips codes defining</span>
<span class="sd">            counties to include in the study area.</span>
<span class="sd">        msa_fips : list or str, optional</span>
<span class="sd">            string or list of strings of fips codes defining</span>
<span class="sd">            MSAs to include in the study area.</span>
<span class="sd">        fips : list or str, optional</span>
<span class="sd">            string or list of strings of five-digit fips codes defining</span>
<span class="sd">            counties to include in the study area.</span>
<span class="sd">        boundary : geopandas.GeoDataFrame, optional</span>
<span class="sd">            geodataframe that defines the total extent of the study area.</span>
<span class="sd">            This will be used to clip tracts lazily by selecting all</span>
<span class="sd">            `GeoDataFrame.representative_point()`s that intersect the</span>
<span class="sd">            boundary gdf</span>
<span class="sd">        years : list of ints, required</span>
<span class="sd">            list of years to include in the study data</span>
<span class="sd">            (the default is [1990, 2000, 2010]).</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Community</span>
<span class="sd">            Community with unharmonized census data</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">years</span> <span class="o">==</span> <span class="s2">&quot;all&quot;</span><span class="p">:</span>
            <span class="n">years</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1990</span><span class="p">,</span> <span class="mi">2000</span><span class="p">,</span> <span class="mi">2010</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">years</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">)):</span>
            <span class="n">years</span> <span class="o">=</span> <span class="p">[</span><span class="n">years</span><span class="p">]</span>

        <span class="n">msa_states</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">msa_fips</span><span class="p">:</span>
            <span class="n">msa_states</span> <span class="o">+=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">msa_definitions</span><span class="p">()[</span>
                <span class="n">datasets</span><span class="o">.</span><span class="n">msa_definitions</span><span class="p">()[</span><span class="s2">&quot;CBSA Code&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="n">msa_fips</span>
            <span class="p">][</span><span class="s2">&quot;stcofips&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="n">msa_states</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">msa_states</span><span class="p">]</span>

        <span class="c1"># build a list of states in the dataset</span>
        <span class="n">allfips</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="n">state_fips</span><span class="p">,</span> <span class="n">county_fips</span><span class="p">,</span> <span class="n">fips</span><span class="p">,</span> <span class="n">msa_states</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">i</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,)):</span>
                    <span class="n">i</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">each</span> <span class="ow">in</span> <span class="n">i</span><span class="p">:</span>
                    <span class="n">allfips</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">each</span><span class="p">[:</span><span class="mi">2</span><span class="p">])</span>
        <span class="n">states</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">allfips</span><span class="p">))</span>

        <span class="c1"># if using a boundary there will be no fips, so reset states to None</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">states</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">states</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="n">df_dict</span> <span class="o">=</span> <span class="p">{</span>
            <span class="mi">1990</span><span class="p">:</span> <span class="n">datasets</span><span class="o">.</span><span class="n">tracts_1990</span><span class="p">(</span><span class="n">states</span><span class="o">=</span><span class="n">states</span><span class="p">),</span>
            <span class="mi">2000</span><span class="p">:</span> <span class="n">datasets</span><span class="o">.</span><span class="n">tracts_2000</span><span class="p">(</span><span class="n">states</span><span class="o">=</span><span class="n">states</span><span class="p">),</span>
            <span class="mi">2010</span><span class="p">:</span> <span class="n">datasets</span><span class="o">.</span><span class="n">tracts_2010</span><span class="p">(</span><span class="n">states</span><span class="o">=</span><span class="n">states</span><span class="p">),</span>
        <span class="p">}</span>

        <span class="n">tracts</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">year</span> <span class="ow">in</span> <span class="n">years</span><span class="p">:</span>
            <span class="n">tracts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">df_dict</span><span class="p">[</span><span class="n">year</span><span class="p">])</span>
        <span class="n">tracts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">(</span><span class="n">tracts</span><span class="p">,</span> <span class="n">sort</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">boundary</span><span class="p">,</span> <span class="n">gpd</span><span class="o">.</span><span class="n">GeoDataFrame</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">boundary</span><span class="o">.</span><span class="n">crs</span> <span class="o">!=</span> <span class="n">tracts</span><span class="o">.</span><span class="n">crs</span><span class="p">:</span>
                <span class="n">warn</span><span class="p">(</span>
                    <span class="s2">&quot;Unable to determine whether boundary CRS is WGS84 &quot;</span>
                    <span class="s2">&quot;if this produces unexpected results, try reprojecting&quot;</span>
                <span class="p">)</span>
            <span class="n">tracts</span> <span class="o">=</span> <span class="n">tracts</span><span class="p">[</span>
                <span class="n">tracts</span><span class="o">.</span><span class="n">representative_point</span><span class="p">()</span><span class="o">.</span><span class="n">intersects</span><span class="p">(</span><span class="n">boundary</span><span class="o">.</span><span class="n">unary_union</span><span class="p">)</span>
            <span class="p">]</span>
            <span class="n">gdf</span> <span class="o">=</span> <span class="n">tracts</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="k">else</span><span class="p">:</span>

            <span class="n">gdf</span> <span class="o">=</span> <span class="n">_fips_filter</span><span class="p">(</span>
                <span class="n">state_fips</span><span class="o">=</span><span class="n">state_fips</span><span class="p">,</span>
                <span class="n">county_fips</span><span class="o">=</span><span class="n">county_fips</span><span class="p">,</span>
                <span class="n">msa_fips</span><span class="o">=</span><span class="n">msa_fips</span><span class="p">,</span>
                <span class="n">fips</span><span class="o">=</span><span class="n">fips</span><span class="p">,</span>
                <span class="n">data</span><span class="o">=</span><span class="n">tracts</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">gdf</span><span class="o">=</span><span class="n">gdf</span><span class="p">,</span> <span class="n">harmonized</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span></div>

<div class="viewcode-block" id="Community.from_lodes"><a class="viewcode-back" href="../../generated/geosnap.Community.from_lodes.html#geosnap.Community.from_lodes">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_lodes</span><span class="p">(</span>
        <span class="bp">cls</span><span class="p">,</span>
        <span class="n">state_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">county_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">msa_fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">fips</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">boundary</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">years</span><span class="o">=</span><span class="mi">2015</span><span class="p">,</span>
        <span class="n">dataset</span><span class="o">=</span><span class="s2">&quot;wac&quot;</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new Community from Census LEHD/LODES data.</span>

<span class="sd">           Instantiate a new Community from LODES data.</span>
<span class="sd">           Pass lists of states, counties, or any</span>
<span class="sd">           arbitrary FIPS codes to create a community. All fips code arguments</span>
<span class="sd">           are additive, so geosnap will include the largest unique set.</span>
<span class="sd">           Alternatively, you may provide a boundary to use as a clipping</span>
<span class="sd">           feature.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        state_fips : list or str, optional</span>
<span class="sd">            string or list of strings of two-digit fips codes defining states</span>
<span class="sd">            to include in the study area.</span>
<span class="sd">        county_fips : list or str, optional</span>
<span class="sd">            string or list of strings of five-digit fips codes defining</span>
<span class="sd">            counties to include in the study area.</span>
<span class="sd">        msa_fips : list or str, optional</span>
<span class="sd">            string or list of strings of fips codes defining</span>
<span class="sd">            MSAs to include in the study area.</span>
<span class="sd">        fips : list or str, optional</span>
<span class="sd">            string or list of strings of five-digit fips codes defining</span>
<span class="sd">            counties to include in the study area.</span>
<span class="sd">        boundary : geopandas.GeoDataFrame, optional</span>
<span class="sd">            geodataframe that defines the total extent of the study area.</span>
<span class="sd">            This will be used to clip tracts lazily by selecting all</span>
<span class="sd">            `GeoDataFrame.representative_point()`s that intersect the</span>
<span class="sd">            boundary gdf</span>
<span class="sd">        years : list of ints, required</span>
<span class="sd">            list of years to include in the study data</span>
<span class="sd">            (the default is 2015).</span>
<span class="sd">        dataset : str, required</span>
<span class="sd">            which LODES dataset should be used to create the Community.</span>
<span class="sd">            Options are &#39;wac&#39; for workplace area characteristics or &#39;rac&#39; for</span>
<span class="sd">            residence area characteristics. The default is &quot;wac&quot; for workplace.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Community</span>
<span class="sd">            Community with LODES data</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">years</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,)):</span>
            <span class="n">years</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">years</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">years</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,)):</span>
            <span class="n">years</span> <span class="o">=</span> <span class="p">[</span><span class="n">years</span><span class="p">]</span>
        <span class="n">years</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">years</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">msa_fips</span><span class="p">:</span>
            <span class="n">msa_counties</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">msa_definitions</span><span class="p">()[</span>
                <span class="n">datasets</span><span class="o">.</span><span class="n">msa_definitions</span><span class="p">()[</span><span class="s2">&quot;CBSA Code&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="n">msa_fips</span>
            <span class="p">][</span><span class="s2">&quot;stcofips&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">msa_counties</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># build a list of states in the dataset</span>
        <span class="n">allfips</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">stateset</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="n">state_fips</span><span class="p">,</span> <span class="n">county_fips</span><span class="p">,</span> <span class="n">msa_counties</span><span class="p">,</span> <span class="n">fips</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">i</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,)):</span>
                    <span class="n">i</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">each</span> <span class="ow">in</span> <span class="n">i</span><span class="p">:</span>
                    <span class="n">allfips</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">each</span><span class="p">)</span>
                    <span class="n">stateset</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">each</span><span class="p">[:</span><span class="mi">2</span><span class="p">])</span>
            <span class="n">states</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">stateset</span><span class="p">))</span>

        <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">year</span> <span class="o">&lt;</span> <span class="mi">2010</span> <span class="k">for</span> <span class="n">year</span> <span class="ow">in</span> <span class="n">years</span><span class="p">):</span>
            <span class="n">gdf00</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">blocks_2000</span><span class="p">(</span><span class="n">states</span><span class="o">=</span><span class="n">states</span><span class="p">,</span> <span class="n">fips</span><span class="o">=</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">allfips</span><span class="p">)))</span>
            <span class="n">gdf00</span> <span class="o">=</span> <span class="n">gdf00</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;year&quot;</span><span class="p">])</span>
            <span class="n">gdf00</span> <span class="o">=</span> <span class="n">_fips_filter</span><span class="p">(</span>
                <span class="n">state_fips</span><span class="o">=</span><span class="n">state_fips</span><span class="p">,</span>
                <span class="n">county_fips</span><span class="o">=</span><span class="n">county_fips</span><span class="p">,</span>
                <span class="n">msa_fips</span><span class="o">=</span><span class="n">msa_fips</span><span class="p">,</span>
                <span class="n">fips</span><span class="o">=</span><span class="n">fips</span><span class="p">,</span>
                <span class="n">data</span><span class="o">=</span><span class="n">gdf00</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">boundary</span><span class="p">,</span> <span class="n">gpd</span><span class="o">.</span><span class="n">GeoDataFrame</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">boundary</span><span class="o">.</span><span class="n">crs</span> <span class="o">!=</span> <span class="n">gdf00</span><span class="o">.</span><span class="n">crs</span><span class="p">:</span>
                    <span class="n">warn</span><span class="p">(</span>
                        <span class="s2">&quot;Unable to determine whether boundary CRS is WGS84 &quot;</span>
                        <span class="s2">&quot;if this produces unexpected results, try reprojecting&quot;</span>
                    <span class="p">)</span>
                <span class="n">gdf00</span> <span class="o">=</span> <span class="n">gdf00</span><span class="p">[</span><span class="n">gdf00</span><span class="o">.</span><span class="n">representative_point</span><span class="p">()</span><span class="o">.</span><span class="n">intersects</span><span class="p">(</span><span class="n">boundary</span><span class="o">.</span><span class="n">unary_union</span><span class="p">)]</span>

        <span class="n">gdf</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">.</span><span class="n">blocks_2010</span><span class="p">(</span><span class="n">states</span><span class="o">=</span><span class="n">states</span><span class="p">,</span> <span class="n">fips</span><span class="o">=</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">allfips</span><span class="p">)))</span>
        <span class="n">gdf</span> <span class="o">=</span> <span class="n">gdf</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;year&quot;</span><span class="p">])</span>
        <span class="n">gdf</span> <span class="o">=</span> <span class="n">_fips_filter</span><span class="p">(</span>
            <span class="n">state_fips</span><span class="o">=</span><span class="n">state_fips</span><span class="p">,</span>
            <span class="n">county_fips</span><span class="o">=</span><span class="n">county_fips</span><span class="p">,</span>
            <span class="n">msa_fips</span><span class="o">=</span><span class="n">msa_fips</span><span class="p">,</span>
            <span class="n">fips</span><span class="o">=</span><span class="n">fips</span><span class="p">,</span>
            <span class="n">data</span><span class="o">=</span><span class="n">gdf</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">boundary</span><span class="p">,</span> <span class="n">gpd</span><span class="o">.</span><span class="n">GeoDataFrame</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">boundary</span><span class="o">.</span><span class="n">crs</span> <span class="o">!=</span> <span class="n">gdf</span><span class="o">.</span><span class="n">crs</span><span class="p">:</span>
                <span class="n">warn</span><span class="p">(</span>
                    <span class="s2">&quot;Unable to determine whether boundary CRS is WGS84 &quot;</span>
                    <span class="s2">&quot;if this produces unexpected results, try reprojecting&quot;</span>
                <span class="p">)</span>
            <span class="n">gdf</span> <span class="o">=</span> <span class="n">gdf</span><span class="p">[</span><span class="n">gdf</span><span class="o">.</span><span class="n">representative_point</span><span class="p">()</span><span class="o">.</span><span class="n">intersects</span><span class="p">(</span><span class="n">boundary</span><span class="o">.</span><span class="n">unary_union</span><span class="p">)]</span>

        <span class="c1"># grab state abbreviations</span>
        <span class="n">names</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">_fipstable</span><span class="p">[</span><span class="n">_fipstable</span><span class="p">[</span><span class="s2">&quot;FIPS Code&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">isin</span><span class="p">(</span><span class="n">states</span><span class="p">)][</span><span class="s2">&quot;State Abbreviation&quot;</span><span class="p">]</span>
            <span class="o">.</span><span class="n">str</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
            <span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">names</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="n">names</span><span class="p">]</span>

        <span class="n">dfs</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">year</span> <span class="ow">in</span> <span class="n">years</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">df</span> <span class="o">=</span> <span class="n">get_lehd</span><span class="p">(</span><span class="n">dataset</span><span class="o">=</span><span class="n">dataset</span><span class="p">,</span> <span class="n">year</span><span class="o">=</span><span class="n">year</span><span class="p">,</span> <span class="n">state</span><span class="o">=</span><span class="n">name</span><span class="p">)</span>
                    <span class="n">df</span><span class="p">[</span><span class="s2">&quot;year&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">year</span>
                    <span class="k">if</span> <span class="n">year</span> <span class="o">&lt;</span> <span class="mi">2010</span><span class="p">:</span>
                        <span class="n">df</span> <span class="o">=</span> <span class="n">gdf00</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s2">&quot;geoid&quot;</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s2">&quot;inner&quot;</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">df</span> <span class="o">=</span> <span class="n">gdf</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s2">&quot;geoid&quot;</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s2">&quot;inner&quot;</span><span class="p">)</span>
                    <span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">set_index</span><span class="p">([</span><span class="s2">&quot;geoid&quot;</span><span class="p">,</span> <span class="s2">&quot;year&quot;</span><span class="p">])</span>
                    <span class="n">dfs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="n">warn</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;{name.upper()} </span><span class="si">{year}</span><span class="s2"> not found!&quot;</span><span class="p">)</span>
                    <span class="k">pass</span>
        <span class="n">out</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">(</span><span class="n">dfs</span><span class="p">,</span> <span class="n">sort</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">out</span> <span class="o">=</span> <span class="n">out</span><span class="p">[</span><span class="o">~</span><span class="n">out</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">duplicated</span><span class="p">(</span><span class="n">keep</span><span class="o">=</span><span class="s2">&quot;first&quot;</span><span class="p">)]</span>
        <span class="n">out</span> <span class="o">=</span> <span class="n">out</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">gdf</span><span class="o">=</span><span class="n">out</span><span class="p">,</span> <span class="n">harmonized</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span></div>

<div class="viewcode-block" id="Community.from_geodataframes"><a class="viewcode-back" href="../../generated/geosnap.Community.from_geodataframes.html#geosnap.Community.from_geodataframes">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_geodataframes</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">gdfs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new Community from a list of geodataframes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        gdfs : list-like of geopandas.GeoDataFrames</span>
<span class="sd">            list of geodataframes that hold attribute and geometry data for</span>
<span class="sd">            a study area. Each geodataframe must have neighborhood</span>
<span class="sd">            attribute data, geometry data, and a time column that defines</span>
<span class="sd">            how the geodataframes are sequenced. The geometries may be</span>
<span class="sd">            stable over time (in which case the dataset is harmonized) or</span>
<span class="sd">            may be unique for each time. If the data are harmonized, the</span>
<span class="sd">            dataframes must also have an ID variable that indexes</span>
<span class="sd">            neighborhood units over time.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">gdf</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">(</span><span class="n">gdfs</span><span class="p">,</span> <span class="n">sort</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">gdf</span><span class="o">=</span><span class="n">gdf</span><span class="p">)</span></div></div>
</pre></div>

    </div>
      
  </div>
</div>
<footer class="footer">
  <div class="container">
    <p class="pull-right">
      <a href="#">Back to top</a>
      
        <br/>
        
      
    </p>
    <p>
        &copy; Copyright 2020, geosnap developers.<br/>
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 2.4.4.<br/>
    </p>
  </div>
</footer>
  </body>
</html>